﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using FX.CC.Andon.Kanban.KanbanSvcRef;
using System.Text.RegularExpressions;

namespace FX.CC.Andon.Kanban
{
    public partial class frmConfig : Form
    {
        IList<Guid> wsIDList = new List<Guid>();
        IList<string> errorCodeList = new List<string>() ;
        KanbanDataSvc svc;

        public frmConfig()
        {
            InitializeComponent();

            string wsIDsStr = ConfigurationManager.AppSettings[Program.listening_line_key];
            string errCodeStr = ConfigurationManager.AppSettings[Program.listening_error_key];
            string resWidthStr = ConfigurationManager.AppSettings[Program.res_width_cfg_key];
            string resHeightStr = ConfigurationManager.AppSettings[Program.res_height_cfg_key];
            if (!string.IsNullOrWhiteSpace(wsIDsStr))
            {
                var wsIDArr = wsIDsStr.Split(Program.split_delimater);
                wsIDList = wsIDArr.Select(p => new Guid(p)).ToList();
            }

            if (!string.IsNullOrWhiteSpace(errCodeStr))
            {
                var errArr = errCodeStr.Split(Program.split_delimater);
                errorCodeList = errArr.Select(p =>p).ToList();
            }

            svc = new KanbanDataSvc();
            var lineArr = svc.GetAllLine();
            var typeArr = svc.GetAllEType();
            foreach (var line in lineArr)
            {
                clbLine.Items.Add(
                    new ItemInfo<Guid>
                    {
                        ItemID = line.Key,
                        ItemDisplayStr = line.Value,
                    });
            }

            //异常
            foreach (var err in typeArr)
            {
                clbError.Items.Add(
                    new ItemInfo<string>
                    {
                        ItemID = err.Key,
                        ItemDisplayStr = err.Value,
                    });
            }

            int idx = 0;
            IList<int> lnCheckedIdxList = new List<int>();
            foreach (var obj in clbLine.Items) {
                Guid itemID = ((ItemInfo<Guid>)obj).ItemID;
                if (wsIDList.Where(p => p == itemID).Count() > 0) {
                    lnCheckedIdxList.Add(idx);
                }
                idx++;
            }

            idx = 0;
            IList<int> errorCheckedIdxList = new List<int>();
            foreach (var obj in clbError.Items)
            {
                string itemID = ((ItemInfo<string>)obj).ItemID;
                if (errorCodeList.Where(p => p == itemID).Count() > 0)
                {
                    errorCheckedIdxList.Add(idx);
                }
                idx++;
            }


            foreach (var i in lnCheckedIdxList) {
                clbLine.SetItemChecked(i, true);
            }

            foreach (var i in errorCheckedIdxList)
            {
                clbError.SetItemChecked(i, true);
            }

            edtWidth.Text = resWidthStr;
            edtHeight.Text = resHeightStr;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string resWidthStr = edtWidth.Text.Trim();
            string resHeightStr = edtHeight.Text.Trim();

            Regex rgx = new Regex(@"^\d+$");
            if (!rgx.Match(resWidthStr).Success) {
                _MsgBox.PopWarning("请输入正确的 分辨率-宽 数值");
                edtWidth.Focus();
                edtWidth.SelectAll();
                return;
            }

            if (!rgx.Match(resHeightStr).Success)
            {
                _MsgBox.PopWarning("请输入正确的 分辨率-高 数值");
                edtHeight.Focus();
                edtHeight.SelectAll();
                return;
            }

            var checkedLineObjs = clbLine.CheckedItems;
            var checkedErrorObjs = clbError.CheckedItems;

            string lineStr = string.Empty;
            foreach(var i in checkedLineObjs){
                lineStr += ((ItemInfo<Guid>)i).ItemID.ToString() + Program.split_delimater;
            }
            lineStr = lineStr.TrimEnd(',');

            string errStr = string.Empty;
            foreach(var i in checkedErrorObjs){
                errStr += ((ItemInfo<string>)i).ItemID + Program.split_delimater;
            }
            errStr = errStr.TrimEnd(',');

            var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.AppSettings.Settings[Program.listening_line_key].Value = lineStr;
            cfg.AppSettings.Settings[Program.listening_error_key].Value = errStr;
            cfg.AppSettings.Settings[Program.res_width_cfg_key].Value = resWidthStr;
            cfg.AppSettings.Settings[Program.res_height_cfg_key].Value = resHeightStr;
            cfg.Save(ConfigurationSaveMode.Modified);
            Application.Restart();
        }

        private void btnClearSession_Click(object sender, EventArgs e)
        {
            var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.AppSettings.Settings[Program.session_id_key].Value = Guid.NewGuid().ToString();
            cfg.Save(ConfigurationSaveMode.Modified);
            Application.Restart();
        }
    }

    public class ItemInfo<K> {
        public K ItemID { get; set; }
        public string ItemDisplayStr { get; set; }

        public override string ToString()
        {
            return this.ItemDisplayStr;
        }
    }
}
